package com.mdlib.helper.module.send.profile.fragments;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.model.entity.MessageEntity;
import com.mdlib.helper.ui.adapter.SendMessageAdapter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/15 17:20
 * 邮箱：hqy_xz@126.com
 */

public class SendMessageFragment extends BaseTitleFragment {


    @Bind(R.id.rv_message_list)
    RecyclerView mRvMessageList;
    @Bind(R.id.sv_send_message)
    SpringView mSvSendMessage;

    private SendMessageAdapter mSendMessageAdapter;

    private List<MessageEntity> messageList = new ArrayList<>();

    private int pageNumber = 0;

    public static SendMessageFragment newInstance() {
        return new SendMessageFragment();
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_send_message;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.me_tv_message, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        init();
    }

    public void init() {
        setReadMessage();
        mSendMessageAdapter = new SendMessageAdapter(messageList);
        mRvMessageList.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvMessageList.setAdapter(mSendMessageAdapter);
        mSvSendMessage.setType(SpringView.Type.FOLLOW);
        mSvSendMessage.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                pageNumber = 0;
                getSendMessage();
            }

            @Override
            public void onLoadmore() {

            }
        });
        mSvSendMessage.setHeader(new HelperHeads(getActivity()));
        getSendMessage();

    }

    /**
     * 获取消息
     */
    public void getSendMessage() {
//        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .getSendMessage(String.valueOf(pageNumber))
                .compose(RxHelper.<ArrayList<MessageEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<MessageEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<MessageEntity> messageEntities) {
//                        stopLoadDialog();
                        pageNumber++;
                        messageList = messageEntities;
                        mSendMessageAdapter.setNewData(messageList);
                        mSendMessageAdapter.notifyDataSetChanged();
                        mSvSendMessage.onFinishFreshAndLoad();

                    }

                    @Override
                    protected void onFail(String msg) {
//                        stopLoadDialog();
                        ToastUtil.showToast(msg);
                    }
                }));
    }

    /**
     * 设置消息为已读
     */
    private void setReadMessage() {
        addSubscribe(DataManager.getInstance().getUserApi()
                .setUnreadMessage()
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {

                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));
    }


}
